package anatex.service;

import java.util.Date;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import anatex.kea.BaseAlgo;
import anatex.kea.genex.GenEx;
import anatex.kea.genex.Genitor;

import anatex.domain.*;
import anatex.service.KeaSettings;
import anatex.service.Settings;
import anatex.translate.TranslateTask;

import java.util.List;
import org.springframework.dao.EmptyResultDataAccessException;

@Service
public class KeaTask {

	private static Integer timeCounter = 0;
	
	@Scheduled(fixedRate=5000)
	@Async
	public void executeKea() {
		System.out.println("In executeKea");
		
		Settings settings = Settings.getConfig();

		if(settings.getKeaServiceEnabled() == false){
			return;
		}else{
			if(KeaTask.timeCounter < settings.getKeaTimeInterval()){
				KeaTask.timeCounter++;
				return;
			}else{
				KeaTask.timeCounter = 0;
			}
		}
		

		Task task = null;
		try {
			task = Task.findTasksByCurrentStateAndEnabled(Task.EnumState.NOT_STARTED,
					true).setFirstResult(0).setMaxResults(1).getSingleResult();
		}catch(EmptyResultDataAccessException ex){
			return;
		}catch (Exception ex) {
			System.out.println("KeaTask error");
			ex.printStackTrace(System.out);
		}
		System.out.println("KeaTask before if ");
		if (task != null){
			
			task.setCurrentState(Task.EnumState.WORKING);
			task.setStartTime(new Date());
			try{
				task = task.merge();
			}catch(Exception ex){
				System.out.println(" merge etc 1");
				ex.printStackTrace();
			}
			System.out.println("start GenEx ");
			
			try {
				Document doc = task.getDocument();
				
				BaseAlgo algo = new GenEx();
				algo.extract(doc, KeywordExtractionModel.findKeywordExtractionModelsByDocumentDomain(doc.getDocumentDomain()).setFirstResult(0).setMaxResults(1).getSingleResult());
				
//				System.exit(1);
			} catch (Exception ex) {
				ex.printStackTrace(System.out);
			}
			
//			try {
//				new GenEx(task.getDocument()).extract();
//			} catch (Exception ex) {
//				System.out.println("GenEx error");
//				ex.printStackTrace(System.out);
//			}
			
			System.out.println("finish GenEx ");
			task.setFinishTime(new Date());
			task.setCurrentState(Task.EnumState.FINISHED);
			System.out.println("finish GenEx 2");
			try{
				task = task.merge();
			}catch(Exception ex){
				System.out.println(" merge etc 2");
				ex.printStackTrace();
			}
			System.out.println("finish GenEx 3");
			
			//Auto create translation task for non-English tasks
			CustomLocale locale = task.getDocument().getCustomLocale();
			System.out.println("finish GenEx 4");
			if(locale.getShort_language().toLowerCase().contains("en") == false){
				TranslateTask transl = new TranslateTask();
				transl.setDocument(task.getDocument());
				transl.setCurrentState(TranslateTask.EnumState.NOT_STARTED);
				transl.setEnabled(true);
				CustomLocale translateTo;
				try{
					translateTo = CustomLocale.findCustomLocalesByLanguage_full(
									"English").setFirstResult(0).setMaxResults(1).getSingleResult();
				}catch(EmptyResultDataAccessException ex){
					translateTo = new CustomLocale();
					translateTo.setShort_language("en");
					translateTo.setShort_country("us");
					translateTo.setCountry_full("United States");
					translateTo.setLanguage_full("English");
					translateTo.persist();
				}
				transl.setLocale(translateTo);
				transl.persist();
			}
			//Info
			System.out.println("Exec time is: "
					+ ((task.getStartTime().getTime() - task.getFinishTime()
							.getTime()) / 1000) + "secs");
		}else{
			System.out.println("No tasks found.");
		}
	 
	}
}

	